package cc.linker.steplink.crm.merchanth5.controller.coupon;

import cc.linker.steplink.crm.commons.coupon.CouponCustomerInfo;
import cc.linker.steplink.crm.commons.coupon.CouponRequest;
import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.pojo.coupon.CustomerCouponVo;
import cc.linker.steplink.crm.merchanth5.service.coupon.CouponH5Service;
//import cc.linker.steplink.crm.merchanth5.service.form.CouponH5Service;
import cc.linker.steplink.crm.merchanth5.service.redis.CouponCacheService;
import cc.linker.steplink.crm.merchanth5.service.redis.MerchantService;
import cc.linker.steplink.crm.response.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: CKQ
 * @DateTime: 2020/8/20 9:15
 * @Description: TODO
 */
@Slf4j
@RestController
@RequestMapping(value = "/h5/coupon/")
public class CouponController {

    @Autowired
    private CouponRequest couponRequest;
    @Autowired
    private RequestFilter requestFilter;

    @Autowired
    private CouponH5Service couponH5Service;
    @Autowired
    private CouponCacheService couponCacheService;
    @Autowired
    private MerchantService merchantService;

    /**
     * @Description：H5公用的优惠券接口请求方法
     * @DateTime：2020/8/20 10:53
     * @Params：
     * @Return：
     */
    @PostMapping("/common")
    public Object commonRequest(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        CouponCustomerInfo couponCustomerInfo = requestFilter.getCouponCustomerInfo(request);
        if (params == null) {
            throw GlobalException.build("请求参数不能为空");
        }
        if (!params.containsKey("active") || params.get("active") == null) {
            throw GlobalException.build("请求方法不能为空");
        }

        if (params.get("active").toString().equals("underway-publish")) {
            return this.couponH5Service.getPublish(params, couponCustomerInfo);
        } else if (params.get("active").toString().equals("customer-coupon-count")) {
            return this.couponH5Service.getCustomerCouponCount(params, couponCustomerInfo);
        } else {
            return couponRequest.doRequest(params.get("active").toString(), params, couponCustomerInfo);
        }
    }

    /**
     * @Description：获取优惠券详情
     * @DateTime：2020/9/8 10:30
     * @Params：
     * @Return：
     */
    @PostMapping("/getCouponDetail")
    public Object getCouponDetail(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        CouponCustomerInfo couponCustomerInfo = requestFilter.getCouponCustomerInfo(request);
        return this.couponH5Service.getCouponDetail(params, couponCustomerInfo);
    }

    /**
     * @Description：用户领取优惠券
     * @DateTime：2020/8/20 13:07
     * @Params：
     * @Return：
     */
    @PostMapping("/receiveConsumption")
    public Object receiveConsumption(@RequestBody Map<String, Object> receiveParam, HttpServletRequest request) {
        CouponCustomerInfo couponCustomerInfo = requestFilter.getCouponCustomerInfo(request);
        return this.couponH5Service.receiveConsumption(receiveParam, couponCustomerInfo, false);
    }

    /**
     * @Description：活动领取优惠券
     * @DateTime：2020/8/20 13:07
     * @Params：
     * @Return：
     */
    @PostMapping("/activityReceiveCoupon")
    public void activityReceiveCoupon(@RequestBody Map<String, Object> receiveParam, HttpServletRequest request) {
        CouponCustomerInfo couponCustomerInfo = requestFilter.getCouponCustomerInfo(request);
        this.couponH5Service.receiveConsumption(receiveParam, couponCustomerInfo, true);
    }

    /**
     * @Description：获取用户的优惠券分页列表
     * @DateTime：2020/8/20 18:43
     * @Params：
     * @Return：
     */
    @PostMapping("/getCustomerCouponPageList")
    public Object getCustomerCouponPageList(@RequestBody Map<String, Object> params,
                                            HttpServletRequest request) {
        CouponCustomerInfo couponCustomerInfo = requestFilter.getCouponCustomerInfo(request);
        return this.couponH5Service.getCustomerCouponPageList(params, couponCustomerInfo);
    }

    /**
     * @Description：获取对应优惠券的优惠券列表
     * @DateTime：2020/8/21 10:26
     * @Params：
     * @Return：
     */
    @GetMapping("/getGoodsCoupon")
    public List<Map<String, Object>> getGoodsCoupon(@RequestParam String goodsId, @RequestParam String customerId, HttpServletRequest request) {
        CouponCustomerInfo couponCustomerInfo = requestFilter.getCouponCustomerInfo(request);
        return this.couponH5Service.getGoodsCoupon(goodsId, customerId, couponCustomerInfo);
    }


    /**
     * @Description：获取优惠券详情
     * @DateTime：2020/8/21 10:41
     * @Params：
     * @Return：
     */
    @GetMapping("/getInfo")
    public Map<String, Object> getInfo(String couponId, HttpServletRequest request) {
        if (StringUtils.isEmpty(couponId)) {
            throw GlobalException.build("参数错误");
        }
        CouponCustomerInfo couponCustomerInfo = requestFilter.getCouponCustomerInfo(request);
        Map<String, Object> param = new HashMap<>();
        param.put("couponId", couponId);
        return this.couponH5Service.getCouponGoodsData(param, couponCustomerInfo);
    }

    /**
     * @Description：根据优惠券获取商品列表
     * @DateTime：2020/8/21 10:41
     * @Params：
     * @Return：
     */
    @GetMapping("/getGoodsListByCouponId")
    public Object getGoodsListByCouponId(@RequestParam String couponId, @RequestParam String creator,
                                         @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                                         @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                         HttpServletRequest request) {
        CouponCustomerInfo couponCustomerInfo = requestFilter.getCouponCustomerInfo(request);
        return couponH5Service.getGoodsListByCouponId(couponId, couponCustomerInfo.getOrgId(), creator, pageIndex, pageSize);
    }

    @GetMapping("/useCustomerCoupon")
    public void useCustomerCoupon(@RequestParam String consumptionId, @RequestParam Integer isYzjCoupon, HttpServletRequest request) {
        CouponCustomerInfo couponCustomerInfo = requestFilter.getCouponCustomerInfo(request);
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        String orgId = wxUser.getOrgId();
        this.couponH5Service.useCustomerCoupon(consumptionId, isYzjCoupon, couponCustomerInfo,customerId,orgId);
    }

    /**
     * @Description：成为核销员
     * @DateTime：2020/8/21 10:41
     * @Params：
     * @Return：
     */
    @PostMapping("/addVerificater")
    public void addVerificater(@RequestBody Map<String, Object> parms, HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        String wxName = wxUser.getNickname();
        String customerId = wxUser.getCustomerId();
        if(StringUtils.isEmpty(parms.get("belongId"))){
            throw GlobalException.build("参数错误！");
        }
        String belongId = parms.get("belongId").toString();
        parms.put("orgId",orgId);
        parms.put("wxName",wxName);
        parms.put("customerId",customerId);
        parms.put("status",0);
        parms.put("isDelete",0);
        parms.put("belongId",belongId);
        Map map = couponH5Service.getVerificaterByCustomerId(customerId,belongId);
        if(map==null || map.size()==0){
            couponH5Service.addVerificater(parms);
        }else {
            couponH5Service.updateVerificater(parms);
        }

    }

    /**
     * 解绑核销员
     * @return
     */
    @PostMapping("/deleteVerificater")
    public void deleteVerificater(HttpServletRequest request){
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        Map params = new HashMap();
        params.put("customerId",customerId);
        couponH5Service.deleteVerificater(params);
    }

    /**
     * @Description：获取详情
     * @DateTime：2020/8/21 10:41
     * @Params：
     * @Return：
     */
    @GetMapping("/getVerificaterInfo")
    public Map getVerificaterInfo( @RequestParam String belongId,HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        Map map = couponH5Service.getVerificaterByCustomerId(customerId,belongId);
        if(map==null || map.size()==0){
            couponH5Service.getVerificaterCountByCustomerId(customerId);
        }
        return map;
    }

    /**
     * 根据核销号查询优惠券
     * @param ： verificateId  核销号
     * @return
     */
    @GetMapping("/getCouponByVerificateId")
    public Object getCouponByVerificateId(String verificateId,Integer isYzjCoupon, HttpServletRequest request){
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        Map verificater = couponH5Service.getPassVerificaterByCustomerId(customerId);
        if(verificater==null || verificater.size()==0){
            throw GlobalException.build("暂无权限");
        }
        Map<String, Object> couponMap;
        if(isYzjCoupon!=null && isYzjCoupon==1){
            CustomerCouponVo customerCouponVo = couponCacheService.getYzjCouponByConsumptionId(verificateId);
            if (customerCouponVo != null) {
                Map<String, Object> storeMap = merchantService.getMerchantDetail(customerCouponVo.getCreateCustomerId(), "");
                customerCouponVo.setStoreLogo(storeMap.get("storeLogo").toString());
                customerCouponVo.setStoreName( storeMap.get("storeName").toString());
            }
            return customerCouponVo;
        }else {
            CouponCustomerInfo couponCustomerInfo = requestFilter.getCouponCustomerInfo(request);
            Map map = new HashMap();
            map.put("consumptionId",verificateId);
            couponMap = couponH5Service.getCouponDetail(map, couponCustomerInfo);
            if(couponMap==null || couponMap.size()==0){
                throw GlobalException.build("没有找到对应卡券");
            }
            couponMap.put("isYzjCoupon",0);
        }
        if(couponMap==null || couponMap.size()==0){
            throw GlobalException.build("没有找到对应卡券");
        }
        return couponMap;
    }
}
